package com.ycxy.school.express.controller;

import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.ycxy.school.express.mapper.AddressMapper;
import com.ycxy.school.express.pojo.dto.AddressAddNewDTO;
import com.ycxy.school.express.pojo.dto.AddressUpdateDTO;
import com.ycxy.school.express.pojo.dto.UserAddNewDTO;
import com.ycxy.school.express.pojo.dto.UserUpdateDTO;
import com.ycxy.school.express.pojo.vo.AddressListItemVO;
import com.ycxy.school.express.pojo.vo.AddressStandardVO;
import com.ycxy.school.express.pojo.vo.RoleListItemVO;
import com.ycxy.school.express.service.IAddressService;
import com.ycxy.school.express.web.JsonResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.hibernate.validator.constraints.Range;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Author jayLuo
 * @Date 2023/1/6 16:17
 * @PackageName:com.ycxy.school.express.controller
 * @ClassName: AddressController
 * @Description: TODO
 * @Version 1.0
 */
@Slf4j
@Api(tags = "03. 地址管理模块")
@RestController
@RequestMapping("/addresses")
public class AddressController {

    @Autowired
    IAddressService addressService;

    // http://localhost:9080/addresses/add-new?name=aa&phone=bb&email=cc
    @ApiOperation("添加地址")
    @ApiOperationSupport(order = 100)
    @PostMapping("/add-new")
    public JsonResult<Void> addNew(AddressAddNewDTO addressAddNewDTO) {
        log.debug("开始处理【添加地址】的请求，参数：{}", addressAddNewDTO);
        addressService.addNew(addressAddNewDTO);
        return JsonResult.ok();
    }

    // http://localhost:9080/addresses/9527/delete
    @ApiOperation("根据id删除地址")
    @ApiOperationSupport(order = 200)
    @ApiImplicitParam(name = "id", value = "地址id", required = true, dataType = "long")
    @PostMapping("/{id:[0-9]+}/delete")
    public JsonResult<Void> delete(@Range(min = 1, message = "删除地址失败，尝试删除的地址ID无效！")
                                   @PathVariable Long id) {
        log.debug("开始处理【根据id删除地址】的请求，参数：{}", id);
        addressService.delete(id);
        return JsonResult.ok();
    }

    // http://localhost:9080/addresses/9527/update
    @ApiOperation("根据id修改地址")
    @ApiOperationSupport(order = 300)
    @ApiImplicitParam(name = "id", value = "地址id", required = true, dataType = "long")
    @PostMapping("/{id:[0-9]+}/update")
    public JsonResult<Void> updateById(@PathVariable Long id, AddressUpdateDTO addressUpdateDTO) {
        log.debug("开始处理【修改地址详情】的请求，参数ID：{}, 新数据：{}", id, addressUpdateDTO);
        addressService.update(id, addressUpdateDTO);
        return JsonResult.ok();
    }

    // http://localhost:9080/addresses
    @ApiOperation("查询地址列表")
    @ApiOperationSupport(order = 420)
    @GetMapping("")
    public JsonResult<List<AddressListItemVO>> list() {
        log.debug("开始处理【根据用户id查询地址列表】的请求");
        List<AddressListItemVO> list = addressService.list();
        return JsonResult.ok(list);
    }

    @ApiOperation("根据id查询地址详细信息")
    @ApiOperationSupport(order = 430)
    @ApiImplicitParam(name = "id", value = "地址id", required = true, dataType = "long")
    @GetMapping("/{id:[0-9]+}/details")
    public JsonResult<AddressStandardVO> getAddressDetailsById(@PathVariable Long id) {
        log.debug("开始处理【根据用户id查询地址列表】的请求");
        AddressStandardVO addressDetails = addressService.getAddressDetailsById(id);
        return JsonResult.ok(addressDetails);
    }
}
